<template>
  <view class="all-feedbacks-container">
    <!-- 头部 -->
    <view class="header">
      <view class="header-left" @click="goBack">
        <text class="back-icon">←</text>
      </view>
      <text class="title">所有反馈</text>
      <view class="header-right">
        <text class="count">{{ allFeedbacks.length }}</text>
      </view>
    </view>

    <!-- 筛选标签 -->
    <view class="filter-tabs">
      <view 
        v-for="tab in filterTabs" 
        :key="tab.key"
        class="tab-item"
        :class="{ active: activeTab === tab.key }"
        @click="switchTab(tab.key)"
      >
        <text class="tab-text">{{ tab.label }}</text>
        <view v-if="tab.count > 0" class="tab-badge">{{ tab.count }}</view>
      </view>
    </view>

    <!-- 加载状态 -->
    <view v-if="loading" class="loading">
      <text>加载中...</text>
    </view>

    <!-- 空状态 -->
    <view v-else-if="filteredFeedbacks.length === 0" class="empty">
      <text class="empty-icon">📝</text>
      <text class="empty-text">{{ getEmptyText() }}</text>
      <button class="add-btn" @click="goToAddFeedback">添加反馈</button>
    </view>

    <!-- 反馈列表 -->
    <view v-else class="feedback-list">
      <view 
        v-for="feedback in filteredFeedbacks" 
        :key="feedback.id" 
        class="feedback-item"
        @click="goToFeedbackDetail(feedback.id)"
      >
        <view class="feedback-header">
          <text class="feedback-content">{{ feedback.content }}</text>
          <view class="status-badge" :class="getStatusClass(feedback.status)">
            {{ getStatusText(feedback.status) }}
          </view>
        </view>
        
        <view class="feedback-meta">
          <text class="feedback-time">{{ formatTime(feedback.createdAt) }}</text>
          <text v-if="feedback.adminRealName" class="feedback-admin">
            反馈给：{{ feedback.adminRealName }}
          </text>
        </view>
        
        <view v-if="feedback.images" class="feedback-images">
          <view 
            v-for="(image, index) in getImageList(feedback.images)" 
            :key="index" 
            class="image-thumb"
          >
            <image :src="image" class="thumb-image" mode="aspectFill" />
          </view>
        </view>
        
        <!-- 管理员回复预览 -->
        <view v-if="feedback.adminReply" class="admin-reply-preview">
          <text class="reply-label">管理员回复：</text>
          <text class="reply-content">{{ feedback.adminReply }}</text>
        </view>
      </view>
    </view>

    <!-- 底部操作 -->
    <view class="bottom-actions">
      <button class="action-btn" @click="goToAddFeedback">
        <text class="btn-icon">+</text>
        <text class="btn-text">添加反馈</text>
      </button>
    </view>
  </view>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import request from '@/utils/request.js'
import tokenManager from '@/utils/token.js'

export default {
  name: 'AllFeedbacks',
  setup() {
    const loading = ref(false)
    const allFeedbacks = ref([])
    const activeTab = ref('all')

    // 筛选标签配置
    const filterTabs = computed(() => {
      const total = allFeedbacks.value.length
      const pending = allFeedbacks.value.filter(f => f.status === 'PENDING').length
      const processing = allFeedbacks.value.filter(f => f.status === 'PROCESSING').length
      const resolved = allFeedbacks.value.filter(f => f.status === 'RESOLVED').length
      const rejected = allFeedbacks.value.filter(f => f.status === 'REJECTED').length

      return [
        { key: 'all', label: '全部', count: total },
        { key: 'pending', label: '待处理', count: pending },
        { key: 'processing', label: '处理中', count: processing },
        { key: 'resolved', label: '已解决', count: resolved },
        { key: 'rejected', label: '已拒绝', count: rejected }
      ]
    })

    // 根据当前标签筛选反馈
    const filteredFeedbacks = computed(() => {
      if (activeTab.value === 'all') {
        return allFeedbacks.value
      }
      return allFeedbacks.value.filter(feedback => feedback.status === activeTab.value.toUpperCase())
    })

    // 获取所有反馈列表
    const fetchAllFeedbacks = async () => {
      if (!tokenManager.isLoggedIn()) {
        uni.showModal({
          title: '提示',
          content: '请先登录后再查看反馈',
          showCancel: false,
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
        return
      }

      loading.value = true
      try {
        const response = await request.request({
          url: '/feedback/list',
          method: 'GET'
        })

        if (response.code === 200) {
          allFeedbacks.value = response.data || []
        } else {
          console.error('获取反馈列表失败:', response)
          uni.showToast({
            title: response.message || '获取失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('获取反馈列表错误:', error)
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
      } finally {
        loading.value = false
      }
    }

    // 切换标签
    const switchTab = (tabKey) => {
      activeTab.value = tabKey
    }

    // 获取图片列表
    const getImageList = (images) => {
      if (!images) return []
      return images.split(',').filter(img => img.trim()).slice(0, 3)
    }

    // 获取状态样式类
    const getStatusClass = (status) => {
      switch (status) {
        case 'PENDING': return 'status-pending'
        case 'PROCESSING': return 'status-processing'
        case 'RESOLVED': return 'status-resolved'
        case 'REJECTED': return 'status-rejected'
        default: return 'status-default'
      }
    }

    // 获取状态文本
    const getStatusText = (status) => {
      switch (status) {
        case 'PENDING': return '待处理'
        case 'PROCESSING': return '处理中'
        case 'RESOLVED': return '已解决'
        case 'REJECTED': return '已拒绝'
        default: return '未知'
      }
    }

    // 获取空状态文本
    const getEmptyText = () => {
      switch (activeTab.value) {
        case 'pending': return '暂无待处理的反馈'
        case 'processing': return '暂无处理中的反馈'
        case 'resolved': return '暂无已解决的反馈'
        case 'rejected': return '暂无已拒绝的反馈'
        default: return '暂无反馈记录'
      }
    }

    // 格式化时间
    const formatTime = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      const now = new Date()
      const diff = now - date
      
      if (diff < 60000) { // 1分钟内
        return '刚刚'
      } else if (diff < 3600000) { // 1小时内
        return Math.floor(diff / 60000) + '分钟前'
      } else if (diff < 86400000) { // 1天内
        return Math.floor(diff / 3600000) + '小时前'
      } else {
        return `${date.getMonth() + 1}-${date.getDate()}`
      }
    }

    // 跳转到反馈详情页面
    const goToFeedbackDetail = (feedbackId) => {
      uni.navigateTo({
        url: `/pages/feedback/feedback-detail?id=${feedbackId}`
      })
    }

    // 跳转到添加反馈页面
    const goToAddFeedback = () => {
      uni.navigateTo({
        url: '/pages/feedback/add-feedback'
      })
    }

    // 返回上一页
    const goBack = () => {
      uni.navigateBack()
    }

    onMounted(() => {
      fetchAllFeedbacks()
    })

    return {
      loading,
      allFeedbacks,
      activeTab,
      filterTabs,
      filteredFeedbacks,
      fetchAllFeedbacks,
      switchTab,
      getImageList,
      getStatusClass,
      getStatusText,
      getEmptyText,
      formatTime,
      goToFeedbackDetail,
      goToAddFeedback,
      goBack
    }
  }
}
</script>

<style scoped>
.all-feedbacks-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: white;
  border-bottom: 2rpx solid #f0f0f0;
}

.header-left, .header-right {
  width: 80rpx;
  display: flex;
  justify-content: center;
}

.back-icon {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.count {
  font-size: 24rpx;
  color: #4CAF50;
  background-color: #e8f4fd;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
}

.filter-tabs {
  display: flex;
  background-color: white;
  padding: 0 20rpx;
  border-bottom: 2rpx solid #f0f0f0;
}

.tab-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20rpx 10rpx;
  position: relative;
}

.tab-item.active {
  color: #4CAF50;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40rpx;
  height: 4rpx;
  background-color: #4CAF50;
  border-radius: 2rpx;
}

.tab-text {
  font-size: 24rpx;
  color: inherit;
}

.tab-badge {
  margin-left: 8rpx;
  background-color: #ff4757;
  color: white;
  font-size: 18rpx;
  padding: 2rpx 8rpx;
  border-radius: 10rpx;
  min-width: 20rpx;
  text-align: center;
}

.loading, .empty {
  text-align: center;
  padding: 100rpx 30rpx;
}

.empty-icon {
  font-size: 80rpx;
  display: block;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  display: block;
  margin-bottom: 30rpx;
}

.add-btn {
  background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
  color: white;
  border: none;
  border-radius: 12rpx;
  padding: 20rpx 40rpx;
  font-size: 26rpx;
}

.feedback-list {
  padding: 20rpx;
  padding-bottom: 120rpx; /* 为底部按钮留出空间 */
}

.feedback-item {
  background-color: white;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.feedback-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16rpx;
}

.feedback-content {
  flex: 1;
  font-size: 26rpx;
  color: #333;
  line-height: 1.4;
  margin-right: 12rpx;
}

.status-badge {
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  font-size: 20rpx;
  white-space: nowrap;
}

.status-pending {
  background-color: #fff3cd;
  color: #856404;
}

.status-processing {
  background-color: #d1ecf1;
  color: #0c5460;
}

.status-resolved {
  background-color: #d4edda;
  color: #155724;
}

.status-rejected {
  background-color: #f8d7da;
  color: #721c24;
}

.feedback-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.feedback-time {
  font-size: 22rpx;
  color: #999;
}

.feedback-admin {
  font-size: 22rpx;
  color: #666;
}

.feedback-images {
  display: flex;
  gap: 12rpx;
  margin-bottom: 16rpx;
}

.image-thumb {
  width: 80rpx;
  height: 80rpx;
  border-radius: 6rpx;
  overflow: hidden;
}

.thumb-image {
  width: 100%;
  height: 100%;
}

.admin-reply-preview {
  background-color: #f8f9fa;
  border-radius: 6rpx;
  padding: 16rpx;
  border-left: 4rpx solid #4CAF50;
}

.reply-label {
  font-size: 22rpx;
  color: #4CAF50;
  font-weight: 500;
}

.reply-content {
  font-size: 24rpx;
  color: #333;
  margin-top: 4rpx;
  display: block;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx;
  background-color: white;
  border-top: 2rpx solid #f0f0f0;
}

.action-btn {
  width: 100%;
  height: 80rpx;
  background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
  border-radius: 12rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon {
  font-size: 32rpx;
  color: white;
  margin-right: 8rpx;
}

.btn-text {
  font-size: 26rpx;
  color: white;
  font-weight: bold;
}
</style>


